package com.kou.dao;

import com.kou.pojo.User;
import com.kou.utils.Base64Utils;
import com.kou.utils.DataSourceUtil;
import com.kou.utils.MD5Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author SteinY
 * @create 2022-03-28 9:10
 * @description:
 */
public class AdminDaoImpl implements AdminDao {
    QueryRunner queryRunner = new QueryRunner();
    @Override
    public User login(String username, String password) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "select * from tb_user where username = ? and password = ?";
        try {
            return queryRunner.query(connection,sql,new BeanHandler<>(User.class),username, MD5Utils.md5(password));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> findAll1() {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "select * from tb_user where status = 1";
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public List<User> findAll2() {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "select * from tb_user where status = 0";
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void deleteById(String id) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "delete from tb_user where id = ? ";
        try {
            queryRunner.update(connection,sql,Integer.parseInt(id));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<User> select(String username, String gender) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "select * from tb_user where username like '%"+username+"%' and gender = ? ";
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(User.class),gender);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
